namespace PriorityQueue
{
    template<class T, class Container = vector<T>, class Compare = my_ComFun::less<T>>
    class priority_queue
    {
    public:
        template<class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
        {
            while (first != last)
            {
                _con.push_back(*first);
                ++first;
            }

            for (int i = (_con.size() - 2) / 2; i >= 0; i--)
            {
                adjustdown(i);
            }
        }

        void adjustdown(size_t parent)
        {
            Compare com;

            size_t child = parent * 2 + 1;
            while (child < size())
            {
                if (child + 1 < size() && com(_con[child], _con[child + 1]))
                    child++;
                if (com(_con[parent], _con[child]))
                {
                    swap(_con[parent], _con[child]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else
                {
                    break;
                }
            }
        }

        void adjustup(size_t child)
        {
            Compare com;

            size_t parent = (child - 1) / 2;
            while (child > 0)
            {
                if (com(_con[parent], _con[child]))
                {
                    swap(_con[parent], _con[child]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else
                    break;
            }
        }

        void push(const T& val)
        {
            _con.push_back(val);
            adjustup(size() - 1);
        }

        void pop()
        {
            swap(_con[0], _con[size() - 1]);
            _con.pop_back();
            adjustdown(0);
        }

        size_t size() const { return _con.size(); }
        bool empty() const { return _con.empty(); }

        T& top() { return _con.front(); }
        const T& top() const { return _con.front(); }

    private:
        Container _con;
    };
}
